package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import modelo.Cosecha;


public class AdmPersistenciaCosecha {

	public static AdmPersistenciaCosecha instancia;

	public static AdmPersistenciaCosecha getinstancia (){
		if(instancia==null)
			instancia = new AdmPersistenciaCosecha();
		return instancia;
	}
	
	public Vector<Cosecha> selectAll(){
		try{
			Vector<Cosecha> resultados = new Vector<Cosecha>();
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			Statement s = connection.createStatement();
			ResultSet rs = s.executeQuery("SELECT * FROM Cosecha");
			while(rs.next()){
					int codigo = rs.getInt(1);
					float cantidad =  rs.getInt(2);
					Date fecha = rs.getDate(3);
					resultados.add(new Cosecha(codigo,cantidad,fecha));
					System.out.println("Se cargo cosecha : "+codigo);
				PoolConnection.getPoolConnecion().releaseConnection(connection);
				return resultados;
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Cosecha select(int codigo){
		try{
		Cosecha c = null;
		Connection connection = PoolConnection.getPoolConnecion().getConnection();
		PreparedStatement s= connection.prepareStatement("select * from Cosecha where codigo = ?");
		s.setLong(1, codigo);
		ResultSet rs = s.executeQuery();
		float cant =  rs.getFloat(2);
		Date fecha = rs.getDate(3);
		c = new  Cosecha(codigo,cant,fecha);
		PoolConnection.getPoolConnecion().releaseConnection(connection);
		return c;
		}
		catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void insert(Cosecha c){
		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("insert into Cosecha (codigo,cantidadcosechada,fecha) values (?,?,?)");
			s.setInt(1, c.getId());
			s.setFloat(2, c.getCantidadCosechadoTotal());
			s.setDate(3, (Date) c.getFechaCosecha());
			s.executeQuery();
			PoolConnection.getPoolConnecion().releaseConnection(connection);
			}
			catch (SQLException e) {
				e.printStackTrace();
			}	
	}
	
	public void update(Cosecha c){
		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("update Cosecha" +
					"set cantidadcosechada = ?, fecha = ? where codigo = ? ");
			s.setInt(3, c.getId());
			s.setFloat(2, c.getCantidadCosechadoTotal());
			s.setDate(1, (Date) c.getFechaCosecha());
			s.executeQuery();
			PoolConnection.getPoolConnecion().releaseConnection(connection);

		}
		catch (SQLException e) {
			e.printStackTrace();
		}	
	}
	
	public void delete(Cosecha c){
		try{
			Connection connection = PoolConnection.getPoolConnecion().getConnection();
			PreparedStatement s= connection.prepareStatement("delete * from cosecha where codigo = ?");
			s.setInt(1, c.getId());
			s.executeQuery();
			PoolConnection.getPoolConnecion().releaseConnection(connection);
			}
			catch (SQLException e) {
				e.printStackTrace();
			}	
	}
	
}


